﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;


namespace SimpleClient
{
    public class Player
    {
        private int id;
        private Point position;
        private PBlock block;
        private int direction;
        public ArrayList bList = new ArrayList();
        public bool alive;
        public Color c;
        public Player(Point p, int hurt)
        {
            direction = 0;
            position = p;
            block = new PBlock();
            block.Position = position;
            alive = true;
            c = Color.Blue;
        }
        public int ID
        {
            get { return id; }
            set { id = value; }
        }
        public void Display(Graphics g)
        {
            block.Display(g);
        }
        public void UnDisplay(Graphics g)
        {
            block.UnDisplay(g);
        }
        public void UpDate(Graphics g)
        {
            block.Display(g);
            MoveBullet();
            foreach (Bullet b in bList)
            {
                b.UpDate(g);
            }
                
        }
        public Point getPosition()
        {
            return position;
        }
        private void MoveBullet()
        {
            for (int i = bList.Count - 1; i >= 0; i--)
            {
                Bullet b = ((Bullet)bList[i]);
                if(b.P.X > Main.xMax || b.P.X < Main.xMin
                    ||b.P.Y < Main.yMin || b.P.Y > Main.yMax)
                {
                    bList.RemoveAt(i);
                }
                if(b.hit)
                {
                    bList.RemoveAt(i);
                    MessageBox.Show("打中了");
                }
            }
        }
        public void Move(Graphics g, int direction)
        {
            if(direction != -1)
                this.direction = direction;
            Point nowP = block.Position;
            int x = nowP.X;
            int y = nowP.Y;
            if(direction == 0)
            {
                y += 5;  
            }
            else if(direction == 1)
            {
                y -= 5;

            }
            else if(direction == 2)
            {
                x += 5;
            }
            else if(direction == 3)
            {
                x -= 5;
            }
            //越界检查
            if (y > Main.yMax - 15)
                y = Main.yMax - 15;
            else if (y < Main.yMin)
                y = Main.yMin;
            else if (x > Main.xMax - 15)
                x = Main.xMax - 15;
            else if (x < Main.xMin)
                x = Main.xMin;
            //碰撞检查
            foreach (Home h in Floor.hList)
            {
                int zx = 2*System.Math.Abs(x - h.Position.X);
                int zy = 2*System.Math.Abs(y - h.Position.Y);
                int disx = 30;
                int disy = 30;
                if(zx < disx && zy < disy)
                {
                    die();
                }
            }
            nowP.X = x;
            nowP.Y = y;
            block.Position = nowP;
            position = block.Position;
        }
        public void attck()
        {
            Bullet b = new Bullet(id); 
            b.Direct = direction; //朝向
            b.P = block.Position;
            //b.move();
            bList.Add(b);
        }
        public void die()
        {
            alive = false;
        }
        
    }
}
